import React from 'react';
import {Menu, Icon} from 'antd';
import {ToolHistory, ToolRoute} from '../utils';
import {configLayout} from '../router/Config';

/**
 * 目录页面：包含工程的所有页面
 */
export default class Home extends React.Component {
  constructor(props) {
    super(props);
    this.list = ToolRoute.formatRouter().routerFormat; //菜单列表
    this.defaultOpenKeys = ['/home']; //菜单的默认key数组
    this.defaultSelectedKeys = ['/home/list']; //菜单项的默认key数组
  }

  //界面渲染函数
  render() {
    return (
      <Menu mode="inline" defaultOpenKeys={this.defaultOpenKeys} defaultSelectedKeys={this.defaultSelectedKeys}>
        {this.renderMenuList()}
      </Menu>
    );
  }

  /**
   * 菜单列表
   */
  renderMenuList() {
    let result = [];
    Object.keys(this.list).forEach((key) => {
      let item = this.list[key];
      let {path, label} = configLayout[key];
      let list = [];
      Object.keys(item).forEach((key2) => {
        let item2 = item[key2];
        if (!item2.view) return;

        let viewItem = (
          <Menu.Item key={item2.keyItem} onClick={() => ToolHistory.href(item2.path)}>
            {item2.label}
          </Menu.Item>
        );
        list.push(viewItem);
      });
      let viewMenu = (
        <Menu.SubMenu
          key={path}
          title={
            <span>
              <Icon type="mail" />
              <span>{label}</span>
            </span>
          }
        >
          {list}
        </Menu.SubMenu>
      );
      result.push(viewMenu);
    });
    return result;
  }
}
